package com.zq.widget.inverted.game.role.impl.plane;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.view.animation.LinearInterpolator;

import com.zq.widget.inverted.game.role.anim.BezierEvaluator;
import com.zq.widget.inverted.game.role.decorate.Decorator;
import com.zq.widget.inverted.game.role.decorate.HPBar;
import com.zq.widget.inverted.game.role.damage.Damage;
import com.zq.widget.inverted.game.role.damage.DamageProxy;
import com.zq.widget.inverted.game.role.anim.Anim;
import com.zq.widget.inverted.game.role.anim.AnimProxy;
import com.zq.widget.inverted.game.role.anim.OnAnimUpdateListener;
import com.zq.widget.inverted.game.role.equipment.EquipmentRole;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.hp.HPProxy;
import com.zq.widget.inverted.game.role.impl.type.Enemy;

/**
 * Created by zhangqiang on 2018/3/29.
 */
public class Plane extends EquipmentRole implements Damage,HP,Enemy{

    private float x1,y1,x2,y2;
    private long duration;
    private HP hpProxy;
    private Damage damageProxy;
    private Decorator decorator;


    public Plane(Context context, int drawableResId, float x1, float y1, float x2, float y2, long duration) {
        super(context, drawableResId);
        this.x1 = x1 - bounds().width()/2;
        this.y1 = y1 - bounds().height()/2;
        this.x2 = x2 + bounds().width()/2;
        this.y2 = y2 + bounds().height()/2;
        this.duration = duration;
        hpProxy = new HPProxy(10);
        damageProxy = new DamageProxy(10);
        decorator = new HPBar(1, Color.RED,10,5,hpProxy);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        decorator.onDraw(canvas,bounds());
    }

    @Override
    protected Anim<?> onCreateAnim() {
        return AnimProxy.ofPointF(new PointF(x1,y1),new PointF(x2,y2))
                .setDuration(duration)
                .setInterpolator(new LinearInterpolator())
                .setTypeEvaluator(new BezierEvaluator())
                .setOnAnimUpdateListener(new OnAnimUpdateListener<PointF>() {
                    @Override
                    public void onAnimUpdate(Anim<PointF> anim) {

                        PointF pointF = anim.getAnimateValue();
                        updateLocation(pointF.x,pointF.y);
                    }
                });
    }

    @Override
    public boolean isFinish() {
        return super.isFinish() || getHp() <= 0;
    }

    @Override
    public long damage() {
        return damageProxy.damage();
    }

    @Override
    public long totalHp() {
        return hpProxy.totalHp();
    }

    @Override
    public long getHp() {
        return hpProxy.getHp();
    }

    @Override
    public boolean addHp(long hp) {
        return hpProxy.addHp(hp);
    }

    @Override
    public boolean reduceHp(long hp) {
        return hpProxy.reduceHp(hp);
    }
}
